package sorting;

import java.util.Arrays;
import java.util.Random;

public class Merge {

	static void  mergeArray(int[] a,int[] b){
		int i = 0 ,j = 0;
		int len1 = a.length;
		int len2 = b.length;
		int len = len1+len2;
		int[] c = new int[len];
		int k = 0;
		while(i<len1 && j <len2){
			if(a[i]<b[j]){
				c[k] = a[i];
				k++;i++;
			}else{
				c[k] = b[j];
				k++;j++;
			}
		}
		if(i>=len1){
			for(;j<len2;j++){
				c[k]=b[j];
				k++;
			}
		}else{
			for(;i<len1;i++){
				c[k]=a[i];
				k++;
			}
		}
		System.out.println("c:"+Arrays.toString(c));
	}
	
	public static void main(String[] args) {
		int len1= 8;
		int len2 = 12;
		int[] a = new int[len1];
		int[] b = new int[len2];
		Random rand = new Random();
		for(int i = 0;i<len1;i++){
			a[i] = rand.nextInt(30);
		}
		for(int i = 0;i<len2;i++){
			b[i] = rand.nextInt(30);
		}
		Arrays.sort(a);
		Arrays.sort(b);
		System.out.println("a:"+Arrays.toString(a));
		System.out.println("b:"+Arrays.toString(b));
		mergeArray(a, b);
		
	}
}
